package com.gjy.nwpufindseats.Fragment;
import java.util.ArrayList;
import com.gjy.nwpufindseats.util.JwNetSpider;
import com.gjy.nwpufindseats.util.NetInfo;
import com.gjy.nwpufindseats.MainActivity;
import com.gjy.nwpufindseats.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class Fragment_room extends Fragment{
	public static NetInfo netInfo = new NetInfo();
	View occupiedRoom;
	
	public static ArrayList<String> ocRoomArrayList;
	Boolean end=false;
	Handler handler = new Handler();
	ProgressBar progressBar;
	Bundle bundle;
	public static SharedPreferences preferences;
	public String xhString;
	public String passwordString;
	public static int loginInfo;
	
	public static Context mainContext;
	private Handler handlerMsg;
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) 
     {
    	bundle = getArguments();
    	mainContext = getActivity();
    	occupiedRoom= inflater.inflate(R.layout.fragment_room, container, false);
    	
    	progressBar = (ProgressBar)occupiedRoom.findViewById(R.id.progressBar);
    	preferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
    	xhString = preferences.getString("xh", "0");//未登录，则返回0，登录返回学号
    	passwordString =  preferences.getString("password", "0");
    
    	if("0".equals(xhString))
    	{
    		Fragment newContent = new Fragment_login();
    		switchFragment(newContent);
    		Toast.makeText(getActivity(), "您还没有登录，请先登录", Toast.LENGTH_SHORT).show();
    		return occupiedRoom;
    	}
    	
    	handlerMsg=new Handler(){
    		@SuppressWarnings("unused")
			private int message;

			public void handleMessage(Message msg){
    		     
    		message = (Integer)msg.obj;
	    		if(loginInfo==0)
				{
					
					Toast.makeText(mainContext, "已保存账户密码错误，请修改", Toast.LENGTH_LONG).show();
				}
				else if(loginInfo==1){
						Toast.makeText(mainContext, "教务网瘫痪了。。。", Toast.LENGTH_SHORT).show();
				}
				else{
					
				}
    		}
    	};
    		 
    	Thread tProgress = new Thread()
    	{
    		public void run()
    		{
    			while (!end) 
                {
                }
    			handler.post(new Runnable()
                {
                     public void run()
                     {
                    	 //progressBar.setVisibility(View.GONE);
                    	 if(end)
                    	 {
               				Bundle bundlef = new Bundle();
               				bundlef.putStringArrayList("room_info", ocRoomArrayList);    
               				bundlef.putString("name", bundle.getString("name"));
               		    	Fragment newContent= new Fragment_room_info();
               		    	newContent.setArguments(bundlef);
               		    	switchFragment(newContent);
                    	 }
                     }
                });  
    		}
    		
    	};
    	tProgress.start();
    	
    	
    	Thread tGet =new Thread()
		{
    		JwNetSpider jwNetSpider=new JwNetSpider();
    		@SuppressWarnings("static-access")
			public void run()
			{
				try 
				{
					ocRoomArrayList=jwNetSpider.getOccupiedRoom(mainContext,xhString, passwordString, bundle.getString("num"));
					if("0".equals(ocRoomArrayList.get(0)))//保存的密码有误
					{
						loginInfo = 0;//跳转至登录信息页面，以修改数据
						Message message = Message.obtain();
						message.obj=loginInfo;
						//通过Handler发布传送消息，handler
						handlerMsg.sendMessage(message);
						Fragment newContent= new Fragment_login_info();
          		    	switchFragment(newContent);
					}
					else if("1".equals(ocRoomArrayList.get(0))){//网页信息有误
						loginInfo = 1;//网站数据错误，跳转至"瘫痪"页面
						Message message = Message.obtain();
						message.obj=loginInfo;
						//通过Handler发布传送消息，handler
						handlerMsg.sendMessage(message);
						Fragment newContent= new Fragment_palsy();
          		    	switchFragment(newContent);
					}
					else{
						end=true;
					}				
				}
				catch ( Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		};
		tGet.start();
    	
		
		
    	return occupiedRoom;
    }
     private void switchFragment(Fragment fragment) {
 		if (getActivity() == null)
 			return;	
 			MainActivity ra = (MainActivity) getActivity();
 			ra.switchContent(fragment);
 		
 	}
}
